package cn.jbolt.wechat.common;

import cn.jbolt.core.enjoy.directive.DateTimeDirective;
import com.jfinal.log.Log;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.jfinal.template.ext.directive.NowDirective;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 公众号 模板消息 引擎工具类
 */
public class JBoltWechatTplMsgEngineUtil {
    private static final Log LOG = Log.getLog(JBoltWechatTplMsgEngineUtil.class);
    private static Map<String,Template> TPL_CACHE_MAP = new HashMap<>();
    private static final String ENGINE_NAME = "jbolt_wechat_tpl_msg_engine";
    private static final String TPL_PATH = "/wechat_msg_tpl";
    public static final JBoltWechatTplMsgEngineUtil me = new JBoltWechatTplMsgEngineUtil();
    private Engine engine;
    private JBoltWechatTplMsgEngineUtil(){
        engine = Engine.create(ENGINE_NAME);
        engine.setToClassPathSourceFactory();
        engine.addDirective("now", NowDirective.class,true);
        engine.addDirective("datetime", DateTimeDirective.class);
    }

    /**
     * 渲染模板
     * @param msgTplFileName 模板文件名 不带后缀
     * @param data
     * @return
     */
    public String render(String msgTplFileName, Map<?, ?> data){
        if(!msgTplFileName.endsWith(".json")){
            msgTplFileName = msgTplFileName +".json";
        }
        String tplPath = TPL_PATH+ File.separator+msgTplFileName;
        Template tpl = TPL_CACHE_MAP.get(tplPath);
        if(tpl == null){
            tpl = engine.getTemplate(tplPath);
            TPL_CACHE_MAP.put(tplPath,tpl);
        }
        if(tpl == null){
            return null;
        }
        return tpl.renderToString(data);
    }

    /**
     * 自定义classPath下的根路径
     * @param classPathBasePath
     * @param msgTplFileName
     * @param data
     * @return
     */
    public String render(String classPathBasePath,String msgTplFileName, Map<?, ?> data){
        if(!msgTplFileName.endsWith(".json")){
            msgTplFileName = msgTplFileName +".json";
        }
        if(classPathBasePath.charAt(0)!='/'){
            classPathBasePath = "/" + classPathBasePath;
        }
        String tplPath = classPathBasePath+ File.separator+msgTplFileName;
        return renderToString(tplPath,data);
    }

    /**
     * 处理渲染和缓存
     * @param tplPath
     * @param data
     * @return
     */
    private String renderToString(String tplPath, Map<?, ?> data){
        Template tpl = load(tplPath);
        String json = tpl.renderToString(data);
        if(json == null){
            throw new RuntimeException("微信模板消息获取失败:"+tplPath);
        }
        json = json.trim();
        if(json.length()==0){
            throw new RuntimeException("微信模板消息获取失败:"+tplPath);
        }
        return json;
    }

    /**
     * 加载模板 进缓存
     * @param tplPath
     * @return
     */
    public Template load(String tplPath) {
        Template tpl = TPL_CACHE_MAP.get(tplPath);
        if(tpl == null){
            tpl = engine.getTemplate(tplPath);
            TPL_CACHE_MAP.put(tplPath,tpl);
        }
        if(tpl == null){
            throw new RuntimeException("微信模板消息模板文件获取失败:"+tplPath);
        }
        return tpl;
    }

    /**
     * 重新清理缓存 重新加载
     * @param msgTplFileName
     * @return
     */
    public Template reload(String msgTplFileName) {
        if(!msgTplFileName.endsWith(".json")){
            msgTplFileName = msgTplFileName +".json";
        }
        String tplPath = TPL_PATH+ File.separator+msgTplFileName;
        return reloadByTplPath(tplPath);
    }

    /**
     * 根据路径 清理缓存 重新加载模板
     * @param tplPath
     */
    private Template reloadByTplPath(String tplPath) {
        if(TPL_CACHE_MAP.containsKey(tplPath)){
            TPL_CACHE_MAP.remove(tplPath);
        }
        Template tpl = engine.getTemplate(tplPath);
        if(tpl == null){
            throw new RuntimeException("微信模板消息模板文件获取失败:"+tplPath);
        }
        TPL_CACHE_MAP.put(tplPath,tpl);
        return tpl;
    }

    /**
     * 重新清理缓存 重新加载
     * @param classPathBasePath
     * @param msgTplFileName
     * @return
     */
    public Template reload(String classPathBasePath,String msgTplFileName) {
        if(!msgTplFileName.endsWith(".json")){
            msgTplFileName = msgTplFileName +".json";
        }
        if(classPathBasePath.charAt(0)!='/'){
            classPathBasePath = "/" + classPathBasePath;
        }
        String tplPath = classPathBasePath+ File.separator+msgTplFileName;
        return reloadByTplPath(tplPath);
    }

    /**
     * 清空缓存
     */
    public void clearAllCache(){
        TPL_CACHE_MAP.clear();
        TPL_CACHE_MAP = new HashMap<>();
    }

}
